package com.axonactive.yolo.dto;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.log4j.Logger;

import com.axonactive.yolo.domain.FlightNotification;

public class FlightNotificationDTO {
	
	private int id;	
	private int userId;
	private String flightId;
	private String flightRoute;
	private String flightDepartureTime;
	private double oldPrice;
	private double newPrice;
	private String currencyCode;
	private int flightChangeType;
	private int status;
	private String saveTime;
	private String driftTime;
	private int isRead;
	
	private final static Logger LOGGER = Logger.getLogger(FlightNotificationDTO.class);
	
	public FlightNotificationDTO() {
		
	}
	
	public FlightNotificationDTO(FlightNotification flightNotification) {
		this.id = flightNotification.getId();
		this.userId = flightNotification.getUserId();
		this.flightId = flightNotification.getFlightId();
		this.flightRoute = flightNotification.getFlightRoute();
		this.flightDepartureTime = flightNotification.getFlightDepartureTime();
		this.oldPrice = flightNotification.getOldPrice();
		this.newPrice = flightNotification.getNewPrice();
		this.currencyCode = flightNotification.getCurrencyCode();
		this.flightChangeType = flightNotification.getFlightChangeType();
		this.status = flightNotification.getStatus();
		this.saveTime = flightNotification.getSaveTime();
		setDriftTime(flightNotification.getSaveTime());
		this.setIsRead(flightNotification.getIsRead());		
	}
	
	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public int getUserId() {
		return userId;
	}

	public void setUserId(int userId) {
		this.userId = userId;
	}

	public String getFlightId() {
		return flightId;
	}

	public void setFlightId(String flightId) {
		this.flightId = flightId;
	}

	public String getFlightRoute() {
		return flightRoute;
	}

	public void setFlightRoute(String flightRoute) {
		this.flightRoute = flightRoute;
	}

	public String getFlightDepartureTime() {
		return flightDepartureTime;
	}

	public void setFlightDepartureTime(String flightDepartureTime) {
		this.flightDepartureTime = flightDepartureTime;
	}

	public double getOldPrice() {
		return oldPrice;
	}

	public void setOldPrice(double oldPrice) {
		this.oldPrice = oldPrice;
	}

	public double getNewPrice() {
		return newPrice;
	}

	public void setNewPrice(double newPrice) {
		this.newPrice = newPrice;
	}

	public String getCurrencyCode() {
		return currencyCode;
	}

	public void setCurrencyCode(String currencyCode) {
		this.currencyCode = currencyCode;
	}

	public int getFlightChangeType() {
		return flightChangeType;
	}

	public void setFlightChangeType(int flightChangeType) {
		this.flightChangeType = flightChangeType;
	}

	public int getStatus() {
		return status;
	}

	public void setStatus(int status) {
		this.status = status;
	}

	public String getSaveTime() {
		return saveTime;
	}

	public void setSaveTime(String saveTime) {
		this.saveTime = saveTime;
	}

	public String getDriftTime() {
		return driftTime;
	}

	public void setDriftTime(String inputTime) {
		String returnValue = "";
		
		try {
			Date oldDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(inputTime);
			Date currentDate = new Date();			
			String []theMonths = {"January", "February", "March", "April", "May",
			          				"June", "July", "August", "September", "October", "November", "December"};
			
			double drift = currentDate.getTime() - oldDate.getTime();
			int seconds = (int) (drift / 1000 % 60);
			int minutes = (int) (drift / (60 * 1000) % 60) ;
			int hours = (int) (drift / (60 * 60 * 1000) % 24);
			int days = (int) (drift / (24 * 60 * 60 * 1000));
			int years = currentDate.getYear() - oldDate.getYear();
			int hour = oldDate.getHours();
			int day = oldDate.getDate();
			int month = oldDate.getMonth();
			String year = inputTime.substring(0, 4);
			
			if (seconds < 60 && minutes == 0 
					&& hours == 0 && days == 0 && years == 0) {
						returnValue = "a few seconds ago";
			} 
            else if (minutes == 1 && hours == 0 
                     && days == 0 && years == 0) {
                returnValue = "about a minute ago";                
            }
			else if (minutes < 60 && hours == 0 
                     && days == 0 && years == 0) {
				returnValue = minutes + " minutes ago";
			} 
            else if (hours == 1 && days == 0
                    && years == 0) {
                returnValue = "about an hour ago";
            }
			else if (hours < 24 && days == 0
                    && years == 0) {
				returnValue = hours + " hours ago";
			}
			else if (days == 1 && years == 0) {            
                if (hour >= 0 && hour < 12) {
                    returnValue = "Yesterday at " + inputTime.substring(11,16) + "am";
                }
                else {
                    returnValue = "Yesterday at " + inputTime.substring(11,16) + "pm";
                }
            }
			 else if (days > 1 && years == 0) { 
                if (hour >= 0 && hour < 12) {
                    returnValue = theMonths[month-1] + " " + day + " at " + inputTime.substring(11,16) + "am";
                }
                else {
                    returnValue = theMonths[month-1] + " " + day + " at " + inputTime.substring(11,16) + "pm";
                }
	        }
			 else if (years >= 1) {
                if (hour >= 0 && hour < 12) {
                    returnValue = theMonths[month-1] + " " + day + ", " + year + " at " + inputTime.substring(11,16) + "am";
                }
                else {
                    returnValue = theMonths[month-1] + " " + day + ", " + year + " at " + inputTime.substring(11,16) + "pm";   
                }
	        }  	
						
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		this.driftTime = returnValue;
	}

	public int getIsRead() {
		return isRead;
	}

	public void setIsRead(int isRead) {
		this.isRead = isRead;
	}
	
	public static void main(String[] args) {
		FlightNotificationDTO f = new FlightNotificationDTO();
		f.setDriftTime("2014-03-27 10:28:54");
		LOGGER.info(f.driftTime);
	}
	
}
